/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumNumbers = function(root) {
    let res = [];
    if(!root)return 0;

    function fun(node, sum) {
        if(node.left)fun(node.left,sum*10+node.left.val);
        if(node.right)fun(node.right,sum*10+node.right.val);

        if(!node.left && !node.right){
            res.push(sum);
            return;
        }
    }
    fun(root, root.val);

    return res.reduce((a,b)=>a+b);
};